package com.klhappy.houyuan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.adapter.AllBaseAdapter;
import com.klhappy.houyuan.adapter.ItemAdapter;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.bean.GoodsAllBean;
import com.klhappy.houyuan.constant.KeyConstant;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.dialog.NetWorkDialog;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.DingDanModle;
import com.klhappy.houyuan.view.AbsActivity;
import com.klhappy.houyuan.view.WholeListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by MrPu on 15/12/18.
 */
public class OrderDetailActivity extends AbsActivity implements CallBackInterModle{
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.tv_time)
    TextView tv_time;
    @InjectView(R.id.tv_sum)
    TextView tv_sum;
    @InjectView(R.id.tv_amount)
    TextView tv_amount;
    @InjectView(R.id.lv_order)
    WholeListView lv_order;

    @OnClick(R.id.click_ra)
    public void click_ra(){finish();}
    @OnClick(R.id.btn_good)
    public void btn_good(){
        modle.setCancel(or_id + "");
        showDialog("取消中...");
    }
    @OnClick(R.id.btn_pay)
    public void btn_pay(){
        Intent intent = new Intent(this ,ConfirmOrderActivity.class);
        intent.putExtra("goods", itemList);
        startActivity(intent);
    }

    private ArrayList<GoodsAllBean> itemList;
    private String  or_id;
    private DingDanModle modle;
    private NetWorkDialog dialog;
    private AllBaseAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.inject(this);
        title.setText("订单详情");

        modle = new DingDanModle(this);
        modle.setCallBackInterModle(this);
        dialog = new NetWorkDialog(this);
        itemList = (ArrayList) this.getIntent().getSerializableExtra("order");
        or_id = (String ) getIntent().getSerializableExtra("or_id");

        tv_time.setText(itemList.get(0).getTime());
        tv_sum.setText("共" + itemList.size() + "样商品");

        float acount = 0;
        for (int i = 0; i < itemList.size(); i ++){
            acount = acount + Float.parseFloat(itemList.get(i).getPrice())*Integer.parseInt(itemList.get(i).getCount());
        }
        tv_amount.setText("合计:  ￥" + acount);
        adapter = new AllBaseAdapter(OrderDetailActivity.this, itemList);
        lv_order.setAdapter(adapter);
        lv_order.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(OrderDetailActivity.this, XiangqingActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("goodId", itemList.get(position).getId() + "");
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.CANCEL_URL)){
            if (o.getCode() == 200){
                adapter.notifyDataSetChanged();
                showToastx(o.getMsg());
                finish();
            }else{
                dialog.netWork(o.getMsg());
                dialog.show();
            }
            closeDialog();
        }
    }
}
